<?php
/**
 * Smarty plugin
 * @package Smarty_ams
 * @subpackage plugins
 */

/**
 * Smarty {html_options_ams} function plugin
 *
 * Type:     function
 * Name:     html_options_ams

 * @author JiangChuangao <2007-01-16 at arouter com>
 * @param array
 *  @aTempOption (array),  array of filled by keys and values,if it is empty,
 *                         output is ""
 *
 *  @bShowid     (bool),   the key will show on or not,initialized value is
 true
 *
 *  @sSeparator  (string), this will be show between key and value. * eg. if
 *                  	   it is ":", show data will be key: value,
 *                         initialized  value is ""
 *
 *  @sSelected   (string), decided the show data by key,initialized value is ""
 *
 *  @bShowBlank  (bool),   the sBlankInit will show or not,initialized value
 *						   is false
 *  							 
 *  @sBlankInit  (string), if set sBlankInit value, the sBlankInit  will show
 *                         or  not will decided by sBlankInit
 *
 * @param Smarty
 * @return       (string), this will be return to smarty template
 */

function smarty_function_html_options_ams($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
	//initial value of the parameter
	
    $bShowid      = false;
    $sSeparator   = "";   
    $sSelected    = "";
    $bShowBlank   = false;
    $sBlankInit   = "";
     
    //output the all options
    $aNewParam   = null;
    $aTempOption = null;
    
    $sExtra ="";
    
    
   //get need parameter from @$params
    foreach($params as $_key => $_val) 
    {
        switch($_key) 
        {
            case 'options':
                $aTempOption = (array)$_val;
                break;
           case 'selected':
                $aNewParam['selected'] = array_map('strval', 
                		array_values((array)$_val));
                break;
           case 'showid':
                $bShowid = (bool)$_val;
                break;
           case 'separator':
            	$sSeparator = (string)$_val;
            	break;     
           case 'showblank':
                $bShowBlank = (bool)$_val;
                break;  
           case 'blankinit':
                $sBlankInit = (string)$_val;
                break;
           default:
                 if(!is_array($_val))
                {
                    $sExtra .= " ".$_key."='";
                    $sExtra .= smarty_function_escape_special_chars($_val)."'";
                }
                else
                {
                	$sError = "html_options: extra attribute '$_key' ";
                	$sError.= "cannot be an array";
                    $smarty->trigger_error($sError, E_USER_NOTICE);
                }
                break;
        }
    }
   
	if ((!isset($aTempOption)) || (!is_array($aTempOption)))
		return '';   //perhaps you can output a error information 
		
	//compose show data 
	if($bShowBlank)
	{
		$aNewParam['options'][''] = $sBlankInit;
		$aNewParam['options'] = $aNewParam['options'] + $aTempOption;	
	}
	else
	{
		$aNewParam['options'] = $aTempOption;
	}
	//$aNewParam['options'] = $aTempOption;
	$aOptions['selected'] = $sSelected;
	if($bShowid)
	{
		smarty_funciton_deal_array($sSeparator, $aNewParam['options']);
	}
    $sOutput = "";
    foreach ($aNewParam['options'] as $_key=>$_val)
    {
        $sOutput .= smarty_function_html_options_optoutput_ams
        			($_key, $_val, isset($aNewParam['selected'])?
        									$aNewParam['selected']:array());
    }
  	
	return $sOutput;
}

//递归处理一个数组，让它的值为其key和value通过一连接符连接起来
function smarty_funciton_deal_array($sSeperator, &$aList)
{
	foreach($aList as $_key => $_val)
	{
		if(is_array($_val))
		{
			smarty_funciton_deal_array($sSeperator, $aList[$_key]);
		}
		else
		{
			$aList[$_key] = $_key. $sSeperator. $_val;
		}
	}
}

//输出一个option
function smarty_function_html_options_optoutput_ams($key, $value, $selected=array())
{
    if(!is_array($value)) 
    {
        $sHtmlOut = '<option value="' .
            smarty_function_escape_special_chars($key) . '"';
        if (in_array((string)$key, $selected))
        {
            $sHtmlOut .= ' selected';
        }
        $sHtmlOut .= '>' . smarty_function_escape_special_chars($value)
        				 . '</option>' . "\n";
    } 
    else
    {
         $sHtmlOut = smarty_function_html_options_optgroup_ams
        				($key, $value, $selected);
    }
    return $sHtmlOut ;
}

//输出一个option组
function smarty_function_html_options_optgroup_ams($key, $values, $sSelected)
{
    $sHtmlOut = "<optgroup label=".$key.">\n";
   	foreach ($values as $_key => $_value)
    {
        $sHtmlOut .= smarty_function_html_options_optoutput_ams
        					($_key, $_value, $sSelected);
    }
    $sHtmlOut .= "</optgroup>\n";
    
    return $sHtmlOut;
}


?>